﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using System.Text.RegularExpressions;
using ESRI.ArcGIS.Controls;

namespace WindowsFormsApplication1
{
    public partial class FrmExportMap : Form
    {
        private IActiveView pActiveView = null;  

        public FrmExportMap(IHookHelper hookHelper)
        {
            InitializeComponent();
            pActiveView = hookHelper.ActiveView; 
        }


        #region 通用函数
        /// <summary>  
        /// 是否大于０的数字  
        /// </summary>  
        /// <param name="v"></param>  
        /// <returns></returns>  
        private bool IsNumbericA(string v)
        {
            return ((this.IsIntegerA(v)) || (this.IsFloatA(v)));
        }
        /// <summary>  
        /// 是否正浮点数  
        /// </summary>  
        /// <param name="v"></param>  
        /// <returns></returns>  
        private bool IsFloatA(string v)
        {
            string pattern = @"^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$";
            Regex reg = new Regex(pattern);
            return reg.IsMatch(v);
        }
        /// <summary>  
        /// 是否正整数  


        /// </summary>  
        /// <param name="v"></param>  
        /// <returns></returns>  
        private bool IsIntegerA(string v)
        {
            string pattern = @"^[0-9]*[1-9][0-9]*$";
            Regex reg = new Regex(pattern);
            return reg.IsMatch(v);
        }
        #endregion 


        private void button1_Click(object sender, EventArgs e)
        {

            this.saveMapFileDialog.Filter = "栅格 JPEG(*.jpg)|*.jpg|栅格 BMP(*.BMP)|*.bmp|栅格 PNG(*.png)|*.png|栅格 TIFF(*.tif)|*.tif|栅格 GIF(*.gif)|*.gif|矢量 SVG(*.svg)|*.svg|矢量 AI(*.ai)|*.ai|矢量 EMF(*.emf)|*.emf|矢量 PDF(*.pdf)|*.pdf|矢量 EPS(*.eps)|*.eps";
            this.saveMapFileDialog.Title = "导出地图";
            this.saveMapFileDialog.RestoreDirectory = true;  //对话框在关闭前是否还原到当前目录下
            this.saveMapFileDialog.ShowDialog();
            this.txtFileName.Text = saveMapFileDialog.FileName;  
        }

         //导出地图
        private void button2_Click(object sender, EventArgs e)
        {
            if (txtResolution.Value > 1)
            {
                ExportTool();
            }
            this.Cursor = Cursors.Default;  
        }

        private void ExportTool()
        {
            IExport pExport = null;
            //  IExportJPEG pExportFormat;  
            IWorldFileSettings pWorldFile = null;
            IExportImage pExportType;
            IEnvelope pEnv = new Envelope() as IEnvelope;
            int lResolution;
            lResolution = Convert.ToInt32(this.txtResolution.Value);
            switch (this.saveMapFileDialog.Filter.ToString().Trim().Substring(0, 3))
            {
                case "jpg":
                    pExport = new ExportJPEG() as IExport;
                    break;
                case "bmp":
                    pExport = new ExportBMP() as IExport;
                    break;
                case "gif":
                    pExport = new ExportGIF() as IExport;
                    break;
                case "tif":
                    pExport = new ExportTIFF() as IExport;
                    break;
                case "png":
                    pExport = new ExportPNG() as IExport;
                    break;
                case "emf":
                    pExport = new ExportEMF() as IExport;
                    break;
                case "pdf":
                    pExport = new ExportPDF() as IExport;
                    break;
                case ".ai":
                    pExport = new ExportAI() as IExport;
                    break;
                case "svg":
                    pExport = new ExportSVG() as IExport;
                    break;
                case "ps":
                    pExport = new ExportPS() as IExport;
                    break;
                default:
                    pExport = new ExportJPEG() as IExport;
                    break;
            }

            if (this.txtFileName.Text.ToString().Trim() != "")
            {
                if (System.IO.File.Exists(this.txtFileName.Text.ToString()) == true)
                {
                    MessageBox.Show("该文件已经存在，请重新命名！");
                    this.txtFileName.Text = "";
                    this.txtFileName.Focus();
                }
                else
                {
                    pExport.ExportFileName = this.txtFileName.Text;
                    pExport.Resolution = lResolution;
                    pExportType = pExport as IExportImage;
                    pExportType.ImageType = esriExportImageType.esriExportImageTypeTrueColor;
                    pEnv = pActiveView.Extent;
                    pWorldFile = (IWorldFileSettings)pExport;
                    pWorldFile.MapExtent = pEnv;
                    pWorldFile.OutputWorldFile = false;
                   
                    tagRECT deviceRECT;
                    deviceRECT = Form1.m_mapControl.ActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame();

                    IEnvelope pDriverBounds;
                    pDriverBounds = new Envelope() as IEnvelope;
                    pDriverBounds.PutCoords(deviceRECT.left, deviceRECT.bottom, deviceRECT.right, deviceRECT.top);
                    pExport.PixelBounds = pDriverBounds;

                    ITrackCancel pTrackCancel = new TrackCancel();
                    Form1.m_mapControl.ActiveView.Output(pExport.StartExporting(), lResolution, ref deviceRECT, pActiveView.Extent, pTrackCancel);
   
                    pExport.FinishExporting();
                    MessageBox.Show("打印图片保存成功!", "保存", MessageBoxButtons.OK);
                    this.Close();

                }

            }
            else
            {
                MessageBox.Show("请保存文件!");
            }

        }

     
  
        private void ExportMap_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                this.Hide();
            }
        }  
    }
}
